package com.example.mybatisplusdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplusdemo.common.utls.SessionUtils;
import com.example.mybatisplusdemo.mapper.OrderMapper;
import com.example.mybatisplusdemo.model.domain.GoodOrder;
import com.example.mybatisplusdemo.model.domain.User;
import com.example.mybatisplusdemo.model.vo.OrderDetailVo;
import com.example.mybatisplusdemo.model.vo.OrderVo;
import com.example.mybatisplusdemo.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-07-15
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, GoodOrder> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Page<GoodOrder> getPage(Page<GoodOrder> page, QueryWrapper<GoodOrder> queryWrapper) {
        return orderMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional
    public boolean createOrder(GoodOrder order) {

        // 生成订单编号
        order.setOrderNo(generateOrderNo());
        // 设置订单状态为待支付
        order.setStatus(0);
        // 设置创建时间
        User u = SessionUtils.getCurrentUserInfo();
        order.setUserId(u.getUserid());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());

        return this.save(order);
    }

    @Override
    @Transactional
    public boolean cancelOrder(Integer orderId) {
        GoodOrder order = this.getById(orderId);
        if (order != null && order.getStatus() == 0) {
            order.setStatus(2); // 设置为已取消
            order.setUpdateTime(new Date());
            return this.updateById(order);
        }
        return false;
    }

    @Override
    @Transactional
    public boolean payOrder(Integer orderId) {
        GoodOrder order = this.getById(orderId);
        if (order != null && order.getStatus() == 0) {
            order.setStatus(1); // 设置为已支付
            order.setUpdateTime(new Date());
            return this.updateById(order);
        }
        return false;
    }

    @Override
    public OrderDetailVo getOrderDetail(Integer orderId) {
        return orderMapper.selectOrderDetailById(orderId);
    }

    @Override
    public List<OrderVo> getOrderListByUserId(Integer userId) {
        return orderMapper.selectOrderListByUserId(userId);
    }

    @Override
    public List<OrderVo> getOrderListByStatus(Integer status) {
        return orderMapper.selectOrderListByStatus(status);
    }

    // 生成订单编号
    private String generateOrderNo() {
        return "ORD" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 4).toUpperCase();
    }
}
